package com.lemon.config;

import com.lemon.main.MainClass;
import com.lemon.utils.ZKUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * @Author liuli
 * @Date 2020/10/11 1:33
 */
public class Config {
    private static final Logger logger = LoggerFactory.getLogger(Config.class);

    /**
     * -------------------------模块私有配置--------------------------
     */
    public static int servicePort;

    public static int minPool;

    public static int maxPool;

    public static int maxIdleTime;

    public static String zkAdds = null;

    public static String globalPath = null;

    public static int maxTotal = 20;

    public static int maxIdle = 5;

    /**
     * -------------------------公有配置--------------------------
     */
    public static String redisCluster = null;

    public static String mysqlUrl = null;

    public static String mysqlUser = null;

    public static String mysqlPwd = null;

    public static String mysqlDriver = null;

    public static void init() {
        try {
            Properties properties = new Properties();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    MainClass.class
                            .getClassLoader()
                            .getResourceAsStream("config.properties"));
            properties.load(inputStreamReader);
            servicePort = Integer.parseInt(properties.getProperty("service.port"));
            minPool = Integer.parseInt(properties.getProperty("minPool"));
            maxPool = Integer.parseInt(properties.getProperty("maxPool"));
            maxIdleTime = Integer.parseInt(properties.getProperty("maxIdleTime"));
            maxTotal = Integer.parseInt(properties.getProperty("maxTotal"));
            maxIdle = Integer.parseInt(properties.getProperty("maxIdle"));
            zkAdds = System.getProperty("zk");
            globalPath = properties.getProperty("globalZkPath");
            // 全局配置
            ZKUtil zkUtil = new ZKUtil(zkAdds);
            String config = zkUtil.getDate(globalPath);
            zkUtil.close();
            Document doc = Jsoup.parse(config);
            Element redis = doc.select("global redis").first();
            redisCluster = redis.select("[name=redis.cluster]").val();
            Element mysql = doc.select("global mysql master").first();
            mysqlUrl = mysql.select("[name=url]").val();
            mysqlUser = mysql.select("[name=username]").val();
            mysqlPwd = mysql.select("[name=password]").val();
            mysqlDriver = mysql.select("[name=driver.class]").val();
            logger.info("redisCluster:{}, mysqlUrl:{}, mysqlUser:{}, mysqlPwd:{}, mysqlDriver:{}",
                    redisCluster, mysqlUrl, mysqlUser, mysqlPwd, mysqlDriver);
        } catch (Exception e) {
            logger.error("config init error.", e);
            System.exit(1);
        }
    }
}
